﻿using System;
using System.Collections;
using System.Web.UI;
using AMS.Core.Entities;
using VitCode.Castle.DataAccess;
using VitCode.Castle.Helper;
using VitCode.Castle.Management;

namespace AMS.Web.BaseData
{
    public partial class AreaEdit : Page
    {
        readonly BaseDataManager help = new BaseDataManager();
        readonly CastleAccess acc = new CastleAccess();

        private string MenuId
        {
            get
            {

                if (ViewState["MenuId"] != null)
                    return ViewState["MenuId"].ToString();
                else
                    return "";
            }
            set { ViewState["MenuId"] = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["MenuId"] != null)
                    MenuId = Request["MenuId"].Trim(); 
                labelId.Text = publ.GetUrlToReceive(Request.QueryString["id"].Trim());
                labelLog.Text = (Request.QueryString["operater"].Trim() == "Insert" ? "新增区域" : "编辑区域");

                if (Request.QueryString["operater"] != null && Request.QueryString["operater"].Trim() == "Update")
                {
                    var objList = acc.GetObjectList(new Area().GetType().Name, "Id", publ.GetUrlToReceive(Request.QueryString["id"].Trim()));
                    if (objList != null && objList.Count > 0)
                    {
                        Area currentArea = (Area) objList[0];

                        ZLTextBox_Name.Text = currentArea.Name;
                        ZLTextBox_Code.Text = currentArea.Code;
                        ZLTextBox_Memo.Text = currentArea.Memo;
                    }
                }
            }

        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            var objArea = new Area();
            string strName = ZLTextBox_Name.Text.Trim();
            string strCode = ZLTextBox_Code.Text.Trim();
            string strMemo = ZLTextBox_Memo.Text.Trim();
            string strParentId = labelId.Text.Trim();

            if (labelId.Text.Trim().Length == 36)
            {
                var lstObj = acc.GetObjectList(new Area().GetType().Name, "Id", strParentId);
                if (lstObj != null && lstObj.Count > 0)
                {
                    if (labelLog.Text.Trim() == "新增区域") //新增
                    {
                        
                        var lstObj2 = acc.GetObjectList(new Area().GetType().Name, "Code", strCode);
                        if(lstObj2 != null && lstObj2.Count > 0)
                        {
                            Alert.MessageBox("新增编号有重复，编号为: " + strCode + "  的区域已存在，请检测！！！");
                            return; 
                        }
                        var lstObj3 = acc.GetObjectList(new Area().GetType().Name, "Name", strName);
                        if (lstObj3 != null && lstObj3.Count > 0)
                        {
                            Alert.MessageBox("新增名称有重复，名称为: " + strName + "  的区域已存在，请检测！！！");
                            return;
                        }
                        var currentArea = (Area)(lstObj[0]);
                        objArea.Parent = currentArea;
                    }
                    else //修改
                    {
                        IList list = help.CheckExist("from Area where Code='" + strCode + "' and Id != '" + labelId.Text.Trim() + "'");
                        if(list != null && list.Count > 0)
                        {
                            Alert.MessageBox("已存在编号为: " + strCode + "  的区域，请检测！！！");
                            return;
                        }
                        IList list2 = help.CheckExist("from Area where Name='" + strName + "'and Id !='" + labelId.Text.Trim() + "'");
                        if (list2 != null && list2.Count > 0)
                        {
                            Alert.MessageBox("已存在名称为: " + strName + "  的区域，请检测！！！");
                            return;
                        }
                        objArea = (Area)(lstObj[0]);
                    }

                }

            }
 
            objArea.Name = strName;
            objArea.Code = strCode;
            objArea.Memo = strMemo;
            bool access = help.SaveOrUpdate(objArea);
            if (access)
            { 
                Alert.MessageBox("保存成功!");
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("AreaManagement.aspx?MenuId=" + MenuId, false);
        }
    }
}
